<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="dateRange"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        range-separator="~"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :editable="false"
        class="filter-item"
        @change="changeDate"
      />
      <el-input v-model="listQuery.plateNumber" placeholder="车牌号" class="filter-item" clearable style="width: 200px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button v-if="type == 0" v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="车牌号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.plateNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单开始时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.beginTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.endTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="订单开始经度" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.beginLng }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单开始纬度" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.beginLat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束经度" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.endLng }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束纬度" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.endLat }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="订单里程(公里)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.distance }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.recieveTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getOrder } from '@/api/order';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import { parseTime } from '@/utils';

export default {
  name: 'Order',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        plateNumber: '',
        beginTime: '',
        endTime: ''
      },
      dateRange: [],
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 00:00:00');
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}') + ' 23:59:59');
    this.listQuery.beginTime = this.dateRange[0] + ' 00:00:00';
    this.listQuery.endTime = this.dateRange[1] + ' 23:59:59';
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getOrder(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    changeDate(val) {
      if (val) {
        this.listQuery.beginTime = val[0];
        this.listQuery.endTime = val[1];
      } else {
        this.listQuery.beginTime = null;
        this.listQuery.endTime = null;
      }
    },
    async handleDownload() {
      if (this.total === 0) {
        this.$message.error('暂无数据');
        return;
      }
      this.downloadLoading = true;
      this.listQuery.pageSize = this.total;
      await getOrder(this.listQuery).then(response => {
        this.listQuery.pageSize = 10;
        if (response.data.total === 0) {
          this.$message.error('暂无数据，请重新刷新页面');
          return;
        }
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['车牌号', '订单开始时间', '订单结束时间', '订单里程(公里)', '接收时间'];
          const filterVal = ['plateNumber', 'beginTime', 'endTime', 'distance', 'recieveTime'];
          const data = this.formatJson(response.data.list, filterVal);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '订单'
          });
          this.downloadLoading = false;
          this.$message({
            message: '订单导出成功',
            type: 'success'
          });
        });
      });
    },
    formatJson(list, filterVal) {
      return list.map(v =>
        filterVal.map(j => {
          if (j === 'beginTime' || j === 'endTime' || j === 'recieveTime') {
            return parseTime(v[j], '{y}-{m}-{d} {h}:{i}:{s}');
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
